package action;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import po.User;
import service.PlaceOrderService;
import service.UserService;
//用户的
//预定订单处理 订单状态设置并跳转到订单中心界面
public class PlaceOrderAction extends ActionSupport{
	private int orderID; //由链接传入的id号
	private int userID; //用户的id号
	private List orders;
	private List emps;
	public List getOrders(){
		return orders;
	}
	
	public void setOrders(List order) {
		this.orders = order;
	}

	public List getEmps() {
		return this.emps;
	}
	
	
	public void setParam() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		userID = (int) session.getAttribute("userID");
		System.out.println("得到的用户编号为:"+userID); //测试   //这里应该有一段空值的判定
		
		//对订单号是否为空的判定	
//		if(request.getAttribute("id")==null) {
//			System.out.println("订单编号为空");
//			return;
//		}
		String sId = (String)request.getParameter("id");
		System.out.println("得到的订单编号为:"+sId); //测试
		orderID = Integer.parseInt(sId); //得到订单号
	}
	public void setParamUserID() {
		HttpServletRequest request = ServletActionContext.getRequest();
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		userID = (int) session.getAttribute("userID");
		//System.out.println("得到的用户编号为:"+userID); //测试   //这里应该有一段空值的判定
	}
	
	//用户预约 跳转-用户预约家教发布的课程
	public String invite() {
		setParam();
		PlaceOrderService pos = new PlaceOrderService();
		//订单更新
		//System.out.println("准备进行更新");
		if(pos.place(userID, orderID)) {
			//System.out.println("从招聘页面直接跳转而来，更新了订单状态信息");
		}
		//System.out.println("更新结束");
		if(pos.getOrders(userID)) {
			orders = pos.getList();		
		}	
		return "success";
			
	}
	//用户取消预约
	public String cancel() {
		setParam();
		//System.out.println("request-get-ordID"+orderID);
		PlaceOrderService pos = new PlaceOrderService();
		pos.cancelOrder(orderID);
		return "success";
	}
	//用户发起结束-然后家教确认
	public String finish() {
		setParam();
		PlaceOrderService pos = new PlaceOrderService();
		pos.finishOrder(orderID);
		return "success";
	}
	
	//用户直接访问-添加招聘信息的查看
	public String center() {
		setParamUserID();
		System.out.println("直接访问订单中心");
		PlaceOrderService pos = new PlaceOrderService();
		
		if(pos.getOrders(userID)) {
			orders = pos.getList();		
		}	
		return "success";
	}
	//家教直接访问
	public String tutorCenter() {
		setParamUserID(); //这里设置的其实是家教的id
		System.out.println("家教直接访问订单中心");
		PlaceOrderService pos = new PlaceOrderService();
		//家教查询的订单应该包括0
		if(pos.getOrdersForTutor(userID)) {
			orders = pos.getList();		
			
		}	
		//第二个表的信息-家教的申请的招聘信息
		PlaceOrderService po = new PlaceOrderService();
		if(po.getEmpForTutor(userID)) {
			emps = po.getList();
		}
		
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		//得到用户id 正常登陆取值正常
		HttpSession session = request.getSession();
		int userID = (int) session.getAttribute("userID");
		UserService us = new UserService();
		if(us.isTutor(userID)) {
			return "success";
		}
		request.setAttribute("tip", "请先提交相应材料注册家教！");
		return "noright"; 
		
	}
	
//	//附带用户的详细信息-这个功能放在Search里面
//	private User user;
//	public User getUser() {
//		return this.user;
//	}
}
